package com.cnb.exam.action;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.cnb.exam.model.Student;
import com.cnb.exam.service.ExamService;
import com.cnb.util.MakeXmlHelp;

public class ListStudentAction extends Action{	
	public ListStudentAction(){
		
	}
	
	public ActionForward execute(ActionMapping mapping,ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception
		{
			ExamService examService = (ExamService) WebApplicationContextUtils
				.getRequiredWebApplicationContext(servlet.getServletContext())
				.getBean("examService");
			Student student = new Student();
			int pageNo = Integer.parseInt((String) request.getParameter("pageNo"));
			List<Student> studentList = examService.listStudent(student, pageNo);
			// System.out.println("SIZE:" + studentList.size());
			// Iterator<Student> iter = studentList.iterator();
			String[] properties = {"stuId", "no", "name", "password", "className", "email"};
			int totalCount = 100;
			String xmlString = MakeXmlHelp.listToXml(studentList, properties, totalCount);
			// System.out.println("xmlString:" + xmlString);
			request.setAttribute("xmlString", xmlString);
			
//			while(iter.hasNext()){
//				Student temp = (Student)iter.next();
//				System.out.println(temp.getStuId() + ", " + temp.getAddr());
//			}			
			return mapping.findForward("success");
		}

}
